package com.heima.wemedia.service.impl;

import cn.afterturn.easypoi.cache.manager.IFileLoader;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-03-16
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    /**
     * 修改频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannels(WmChannel wmChannel) {
        //判断频道名称时候为空
        if (StringUtils.isBlank(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道名称有误");
        }

        WmChannel dbWmChannel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (dbWmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已存在");
        }
        //判断时候有描述
       /* if (wmChannel.getDescription() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.)
        }*/
        //判断创建时间
        if (wmChannel.getCreatedTime() == null) {
            wmChannel.setCreatedTime(new Date());
        }


        updateById(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存用户
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannels(WmChannel wmChannel) {
        //判断频道名称时候为空
        if (StringUtils.isBlank(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道名称有误");
        }

        WmChannel dbWmChannel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (dbWmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已存在");
        }
        //判断时候有描述
       /* if (wmChannel.getDescription() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.)
        }*/
        //判断创建时间
        if (wmChannel.getCreatedTime() == null) {
            wmChannel.setCreatedTime(new Date());
        }


        save(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 频道名称模糊分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult checkListChannels(ChannelDto dto) {
        //检验参数
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(WmChannel::getName, dto.getName());
        //按照用户查询
        // lambdaQueryWrapper.eq(WmChannel::getId, WmThreadLocalUtil.getUser().getId());
        //根据创建时间倒序
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page = page(page, lambdaQueryWrapper);
        //结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        //设置条件
        return responseResult;
    }

    /**
     * 平台管理-删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannels(Integer id) {
        WmChannel wmChannel = getById(id);
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "数据不存在");
        }
        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }

    /**
     * 查询所有的频道
     *
     * @return
     */
    @Override
    public ResponseResult getChannels() {
        return ResponseResult.okResult(list());
    }
}
